package org.yate.oa.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.yate.oa.model.LoginEntity;
import org.yate.oa.model.MessageEntity;
import org.yate.oa.model.OrgEntity;
import org.yate.oa.model.PerformanceEntity;
import org.yate.oa.model.PerformanceEntityU;
import org.yate.oa.model.PerformancePartEntity;
import org.yate.oa.model.RelationEntity;
import org.yate.oa.model.UserEntity;
import org.yate.oa.service.IMessageService;
import org.yate.oa.service.IPerformancePartService;
import org.yate.oa.service.IPerformanceService;
import org.yate.oa.service.IPerformanceServiceU;
import org.yate.oa.service.IUserService;
import org.yate.oa.service.imp.LoginService;
import org.yate.oa.service.imp.MessageService;
import org.yate.oa.service.imp.OrgService;
import org.yate.oa.service.imp.PerformancePartService;
import org.yate.oa.service.imp.PerformanceService;
import org.yate.oa.service.imp.PerformanceServiceU;
import org.yate.oa.service.imp.RelationService;
import org.yate.oa.service.imp.UserService;
import org.yate.oa.utils.Constants;
import org.yate.oa.utils.ExportExcel;
import org.yate.oa.utils.Pagination;
import org.yate.oa.utils.PerformanceStatus;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@WebServlet(urlPatterns = { "/performance/get", "/performance/post", "/performance/put", "/performance/delete" }, asyncSupported = false)
public class PerformanceController extends BaseServletController {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String reqType = req.getParameter("type");
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		if (StringUtils.isEmpty(reqType)) {
			json.put("code", "401");
			json.put("msg", "type cannot be null");
			out.println(json);
			out.flush();
			out.close();
			return;
		}
		if (reqType.equals("get")) {
			get(req, res);
		} else if (reqType.equals("makep")) {
			makePerformance(req, res);
		} else if (reqType.equals("getmyps")) {
			getPerformancesByCreater(req, res);
		} else if (reqType.equals("zlgetmyps")) {
			getMyOrgPerformances(req, res);
		} else if (reqType.equals("getmyorgps")) {
			getMyOrgPerformances(req, res);
		} else if (reqType.equals("distributed")) {
			distributed(req, res);
		} else if (reqType.equals("getpbyid")) {
			getPerformanceByPid(req, res);
		} else if (reqType.equals("zp")) {
			performanceZP(req, res);
		} else if (reqType.equals("kp")) {
			performanceKP(req, res);
		} else if (reqType.equals("sh")) {
			performanceSH(req, res);
		} else if (reqType.equals("all")) {
			getPerformances(req, res);
		} else if (reqType.equals("branch")) {
			getAllBranch(req, res);
		} else if (reqType.equals("changestatus")) {
			changeStatus(req, res);
		} else if (reqType.equals("excel")) {
			exportExcel(req, res);
		} else if (reqType.equals("approval")) {
			approval(req, res);
		}
	}

	// 修改绩效考核状态
	private void changeStatus(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		String status = req.getParameter("status");
		Integer pid = Integer.parseInt(req.getParameter("pid") == null ? "0" : req.getParameter("pid"));
		PerformanceEntity pe = new PerformanceEntity();
		pe.setId(pid);
		pe.setStatus(status);
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		psvr.updateStatus(pe);
		json.put("code", "200");
		json.put("msg", "change status success");
		out.println(json);
		out.flush();
		out.close();
	}

	// 获取自己创建的绩效表和目标用户是自己的绩效表
	private void getPerformancesByCreater(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		LoginEntity le = (LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY);
		if (le == null) {
			json.put("code", "401");
			json.put("msg", "current user is null");
			out.println(json);
			out.flush();
			out.close();
			return;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = new PerformanceEntity();
		pe.setCreaterId(le.getUserId());
		pe.setTargetUserId(le.getUserId());
		pe.setPartName(sdf.format(new Date()));
		List<PerformanceEntity> ps = psvr.getEntitiesBy(pe);
		json.put("code", "200");
		json.put("data", ps);
		out.println(json);
		out.flush();
		out.close();
	}

	// 获取本部门所有人员的绩效
	public void getMyOrgPerformances(HttpServletRequest req, HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		Integer ownerId = ((LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY)).getUserId();
		RelationService relationService = this.springCtx.getBean(RelationService.class);
		RelationEntity relationEntity = new RelationEntity();
		relationEntity.setUserId(ownerId);
		// relationEntity = relationService.getEntityBy(relationEntity);
		// List<Integer> ids = new ArrayList<Integer>();
		// Integer orgId = relationEntity.getOrgId();
		// ids.add(orgId);
		// OrgService orgService = this.springCtx.getBean(OrgService.class);
		// queryOrd(ids, orgId, orgService);
		OrgService orgService = this.springCtx.getBean(OrgService.class);
		List<Integer> ids = new ArrayList<Integer>();
		List<RelationEntity> relationEntities = relationService.getEntitiesBy(relationEntity);
		for (RelationEntity re : relationEntities) {
			ids.add(re.getOrgId());
			queryOrd(ids, re.getOrgId(), orgService);
		}

		UserService userService = this.springCtx.getBean(UserService.class);
		List<UserEntity> userList = userService.getAllUsersByOrgId(ids);
		ids.clear();
		for (UserEntity u : userList) {
			ids.add(u.getId());
		}
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		String date = req.getParameter("date") == null ? "" : req.getParameter("date");
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isEmpty(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			map.put("date", sdf.format(new Date()));
		} else {
			map.put("date", date);
		}

		map.put("ids", ids);
		List<PerformanceEntity> pes = psvr.getMyOrgEntities(map);
		json.put("code", "200");
		json.put("data", pes);
		out.println(json);
		out.flush();
		out.close();
	}

	public static void queryOrd(List<Integer> ids, Integer id, OrgService orgService) {
		OrgEntity orgEntity = new OrgEntity();
		orgEntity.setParentOrgId(id);
		List<OrgEntity> orgEntitys = orgService.getEntitiesBy(orgEntity);
		Iterator<OrgEntity> it = orgEntitys.iterator();
		while (it.hasNext()) {
			orgEntity = (OrgEntity) it.next();
			ids.add(orgEntity.getId());
			queryOrd(ids, orgEntity.getId(), orgService);
		}
	}

	// 根据pid获取绩效表
	private void getPerformanceByPid(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		Integer pid = Integer.parseInt(req.getParameter("pid"));
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = psvr.getEntity(pid);
		PerformancePartEntity ppe = new PerformancePartEntity();
		ppe.setPerId(pe.getId());
		// ppe.setType(1);//只加载定量部分
		IPerformancePartService ppsvr = this.springCtx.getBean(PerformancePartService.class);
		List<PerformancePartEntity> parts = ppsvr.getEntitiesBy(ppe);
		IUserService usvr = this.springCtx.getBean(UserService.class);
		json.put("code", "200");
		json.put("pe", pe);
		json.put("target", usvr.getEntity(pe.getTargetUserId()));
		json.put("creater", usvr.getEntity(pe.getCreaterId()));
		json.put("parts", parts);
		out.println(json);
		out.flush();
		out.close();
	}

	// 派发
	private void distributed(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		Integer pid = Integer.parseInt(req.getParameter("pid"));
		PerformanceEntity pe = new PerformanceEntity();
		pe.setId(pid);
		pe.setStatus(PerformanceStatus.distributed.name());
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		psvr.updateStatus(pe);
		json.put("code", "200");
		json.put("msg", "success");
		out.println(json);
		out.flush();
		out.close();
	}

	private void get(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		Integer targetUserId = Integer.parseInt(req.getParameter("targetUserId"));
		LoginEntity le = (LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY);
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = new PerformanceEntity();
		pe.setCreaterId(le.getUserId());
		if (req.getParameter("createrId") != null) {
			Integer createrId = Integer.parseInt(req.getParameter("createrId"));
			if (createrId > 0) {
				pe.setCreaterId(createrId);
			}
		}
		pe.setTargetUserId(targetUserId);
		pe = psvr.getLastPerformance(pe);
		if (pe == null) {
			json.put("code", "200");
			json.put("data", null);
			out.println(json);
			out.flush();
			out.close();
		} else {
			IPerformancePartService ppsvr = this.springCtx.getBean(PerformancePartService.class);
			PerformancePartEntity ppe = new PerformancePartEntity();
			ppe.setPerId(pe.getId());
			ppe.setType(1);
			List<PerformancePartEntity> pps = ppsvr.getEntitiesBy(ppe);
			if (pps != null && pps.size() > 0) {
				json.put("code", "200");
				json.put("data", pps);
				out.println(json);
				out.flush();
				out.close();
			} else {
				json.put("code", "200");
				json.put("data", null);
				out.println(json);
				out.flush();
				out.close();
			}
		}
	}

	private void makePerformance(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		String pName = req.getParameter("pName");
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = new PerformanceEntity();
		pe.setPartName(pName);
		pe = psvr.getEntityBy(pe);
		if (pe != null && !pe.getStatus().equals(PerformanceStatus.initialize.name())) {
			// 当前的绩效考核单已经创建，并且已经提交给目标用户处理了，所以不能再修改了
			json.put("code", "301");
			json.put("pid", pe.getId());
			out.println(json);
			out.flush();
			out.close();
			return;
		}
		if (pe == null) {
			Integer targetUserId = Integer.parseInt(req.getParameter("targetUserId"));
			LoginEntity le = (LoginEntity) req.getSession().getAttribute(Constants.SESSION_USER_ENTITY);
			Long beginPeriod = Long.parseLong(req.getParameter("beginPeriod"));
			Long endPeriod = Long.parseLong(req.getParameter("endPeriod"));
			Long creationDate = Long.parseLong(req.getParameter("creationDate"));
			String status = PerformanceStatus.initialize.name();
			Integer createrId = 0;
			if (req.getParameter("createrId") != null) {
				createrId = Integer.parseInt(req.getParameter("createrId"));
				if (createrId == 0) {
					createrId = le.getUserId();
				}
			} else {
				createrId = le.getUserId();
			}
			pe = new PerformanceEntity(0, pName, createrId, targetUserId, new Date(beginPeriod), new Date(endPeriod), new Date(creationDate), status, Float.valueOf("0"));
			psvr.addEntity(pe);
		} else {
			Integer targetUserId = Integer.parseInt(req.getParameter("targetUserId"));
			Integer createrId = Integer.parseInt(req.getParameter("createrId"));
			pe.setTargetUserId(targetUserId);
			pe.setCreaterId(createrId);
			psvr.update(pe);
			IPerformancePartService pps = this.springCtx.getBean(PerformancePartService.class);
			pps.removeByPerId(pe.getId());
		}
		JSONArray parts = JSON.parseArray(req.getParameter("parts"));
		if (makePerformanceParts(pe.getId(), parts)) {
			json.put("code", "200");
			json.put("msg", "success");
			out.println(json);
			out.flush();
			out.close();
		} else {
			json.put("code", "500");
			json.put("msg", "add faild");
			out.println(json);
			out.flush();
			out.close();
		}
	}

	private boolean makePerformanceParts(Integer perId, JSONArray parts) {
		try {
			PerformancePartEntity ppe;
			IPerformancePartService pps = this.springCtx.getBean(PerformancePartService.class);
			for (int i = 0; i < parts.size(); i++) {
				String partName = parts.getJSONObject(i).getString("pName");
				Integer partScore = Integer.parseInt(parts.getJSONObject(i).getString("pScore"));
				Integer partWeight = Integer.parseInt(parts.getJSONObject(i).getString("pWeight"));
				String partDes = parts.getJSONObject(i).getString("pDes");
				String partStandard = parts.getJSONObject(i).getString("pStandard");
				Float selfScore = Float.parseFloat(parts.getJSONObject(i).getString("selfScore"));
				Float leaderScore = Float.parseFloat(parts.getJSONObject(i).getString("leaderScore"));
				String des = parts.getJSONObject(i).getString("des");
				Integer partType = Integer.parseInt(parts.getJSONObject(i).getString("partType"));
				ppe = new PerformancePartEntity(0, perId, partName, partScore, partWeight, partDes, partStandard, selfScore, leaderScore, leaderScore, des, partType);
				pps.addEntity(ppe);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// 自评
	private void performanceZP(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		JSONArray parts = JSON.parseArray(req.getParameter("parts"));
		// 修改每个part
		IPerformancePartService ppsvr = this.springCtx.getBean(PerformancePartService.class);
		PerformancePartEntity ppe;
		for (int i = 0; i < parts.size(); i++) {
			ppe = new PerformancePartEntity(parts.getJSONObject(i).getInteger("pid"), parts.getJSONObject(i).getInteger("perId"), parts.getJSONObject(i).getString("pName"), parts.getJSONObject(i)
					.getInteger("pScore"), parts.getJSONObject(i).getInteger("pWeight"), parts.getJSONObject(i).getString("pDes"), parts.getJSONObject(i).getString("pStandard"), parts
					.getJSONObject(i).getFloat("selfScore"), parts.getJSONObject(i).getFloat("leaderScore"), parts.getJSONObject(i).getFloat("managerScore"), parts.getJSONObject(i).getString("des"),
					parts.getJSONObject(i).getInteger("partType"));
			ppsvr.update(ppe);
		}
		// 将performance status 改成processing
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = new PerformanceEntity();
		pe.setId(Integer.parseInt(parts.getJSONObject(0).getString("perId")));
		pe.setStatus(PerformanceStatus.processiong.name());
		psvr.updateStatus(pe);
		json.put("code", "200");
		json.put("msg", "success");
		out.println(json);
		out.flush();
		out.close();
	}

	// 考评
	private void performanceKP(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		JSONArray parts = JSON.parseArray(req.getParameter("parts"));
		// 修改每个part
		IPerformancePartService ppsvr = this.springCtx.getBean(PerformancePartService.class);
		PerformancePartEntity ppe;
		for (int i = 0; i < parts.size(); i++) {
			ppe = new PerformancePartEntity(parts.getJSONObject(i).getInteger("pid"), parts.getJSONObject(i).getInteger("perId"), parts.getJSONObject(i).getString("pName"), parts.getJSONObject(i)
					.getInteger("pScore"), parts.getJSONObject(i).getInteger("pWeight"), parts.getJSONObject(i).getString("pDes"), parts.getJSONObject(i).getString("pStandard"), parts
					.getJSONObject(i).getFloat("selfScore"), parts.getJSONObject(i).getFloat("leaderScore"), parts.getJSONObject(i).getFloat("managerScore"), parts.getJSONObject(i).getString("des"),
					parts.getJSONObject(i).getInteger("partType"));
			ppsvr.update(ppe);
		}
		// 将performance status 改成complete
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = new PerformanceEntity();
		pe.setId(Integer.parseInt(parts.getJSONObject(0).getString("perId")));
		pe.setStatus(PerformanceStatus.complete.name());
		psvr.updateStatus(pe);
		json.put("code", "200");
		json.put("msg", "success");
		out.println(json);
		out.flush();
		out.close();
	}

	// 审核
	private void performanceSH(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		JSONArray parts = JSON.parseArray(req.getParameter("parts"));
		Float totalScore = Float.parseFloat(req.getParameter("total"));
		// 修改每个part
		IPerformancePartService ppsvr = this.springCtx.getBean(PerformancePartService.class);
		PerformancePartEntity ppe;
		for (int i = 0; i < parts.size(); i++) {
			ppe = new PerformancePartEntity(parts.getJSONObject(i).getInteger("pid"), parts.getJSONObject(i).getInteger("perId"), parts.getJSONObject(i).getString("pName"), parts.getJSONObject(i)
					.getInteger("pScore"), parts.getJSONObject(i).getInteger("pWeight"), parts.getJSONObject(i).getString("pDes"), parts.getJSONObject(i).getString("pStandard"), parts
					.getJSONObject(i).getFloat("selfScore"), parts.getJSONObject(i).getFloat("leaderScore"), parts.getJSONObject(i).getFloat("managerScore"), parts.getJSONObject(i).getString("des"),
					parts.getJSONObject(i).getInteger("partType"));
			ppsvr.update(ppe);
		}
		// 将performance status 改成completed
		IPerformanceService psvr = this.springCtx.getBean(PerformanceService.class);
		PerformanceEntity pe = new PerformanceEntity();
		pe.setId(Integer.parseInt(parts.getJSONObject(0).getString("perId")));
		pe.setStatus(PerformanceStatus.completed.name());
		pe.setTotalScore(totalScore);
		psvr.updateStatus(pe);
		json.put("code", "200");
		json.put("msg", "success");
		out.println(json);
		out.flush();
		out.close();
	}

	/**
	 * 查询所有的绩效数据
	 * 
	 * @param req
	 * @param res
	 * @throws ServletException
	 * @throws IOException
	 */
	private void getPerformances(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		String orgId = req.getParameter("branch");
		String page = req.getParameter("page");
		String searchVal = req.getParameter("searchVal");
		String searchVal2 = req.getParameter("searchVal2");
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		pageSize = (pageSize <= 0 ? 10 : pageSize);
		int pageIndex = 0;
		if (!StringUtils.isEmpty(page)) {
			pageIndex = Integer.parseInt(page);
		}
		if (!searchVal.equals("") && !searchVal2.equals("")) {
			searchVal += " 23:59:59";
			searchVal2 += " 23:59:59";
		}
		List<Integer> ids = new ArrayList<Integer>();
		if (!orgId.equals("")) {
			OrgService orgService = this.springCtx.getBean(OrgService.class);
			ids.add(Integer.parseInt(orgId));
			queryOrd(ids, Integer.parseInt(orgId), orgService);
		}
		IPerformanceServiceU psvr = this.springCtx.getBean(PerformanceServiceU.class);
		String date = req.getParameter("date");
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isEmpty(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			map.put("date", sdf.format(new Date()));
		} else {
			map.put("date", date);
		}
		map.put("ids", ids.size() == 0 ? null : ids);
		Pagination<PerformanceEntityU> data = psvr.getEntitieSearch(pageIndex, pageSize, "", "", map);
		json.put("status", "200");
		json.put("data", data);
		out.println(json);
		out.flush();
		out.close();
	}

	private void exportExcel(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		res.reset();
		String fileName = "绩效数据" + sdf.format(new Date());
		if (req.getHeader("user-agent").indexOf("MSIE") != -1) {
			fileName = java.net.URLEncoder.encode(fileName, "utf-8") + ".xls";
		} else {
			fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1") + ".xls";
		}
		res.setHeader("Content-disposition", "attachment;filename=" + fileName);
		res.setContentType("application/msexcel;charset=UTF-8");

		String orgId = req.getParameter("branch");
		List<Integer> ids = new ArrayList<Integer>();
		if (!orgId.equals("")) {
			OrgService orgService = this.springCtx.getBean(OrgService.class);
			ids.add(Integer.parseInt(orgId));
			queryOrd(ids, Integer.parseInt(orgId), orgService);
		}
		IPerformanceServiceU psvr = this.springCtx.getBean(PerformanceServiceU.class);
		String date = req.getParameter("date");
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isEmpty(date)) {
			sdf = new SimpleDateFormat("yyyyMM");
			map.put("date", sdf.format(new Date()));
		} else {
			map.put("date", date);
		}
		map.put("ids", ids.size() == 0 ? null : ids);
		List<PerformanceEntityU> list = psvr.getEntitieSearch(map);
		ExportExcel<PerformanceEntityU> ex = new ExportExcel<PerformanceEntityU>();
		OutputStream out = res.getOutputStream();
		String[] headers = { "名称", "考核者", "被考核者", "考核周期开始时间", "考核周期结束时间", "创建时间", "状态", "总分" };
		ex.exportExcel("考核数据" + sdf.format(new Date()) + ".xls", headers, list, out);
		out.close();
	}

	// HR按照部门批量审批
	private void approval(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		String orgId = req.getParameter("branch");
		Integer zlId;
		List<Integer> ids = new ArrayList<Integer>();
		if (!orgId.equals("")) {
			OrgService orgService = this.springCtx.getBean(OrgService.class);
			ids.add(Integer.parseInt(orgId));
			queryOrd(ids, Integer.parseInt(orgId), orgService);
		}
		IPerformanceServiceU psvr = this.springCtx.getBean(PerformanceServiceU.class);
		String date = req.getParameter("date");
		String status = req.getParameter("op").equals("1") ? "hrcompleted" : "completed";
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isEmpty(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			map.put("date", sdf.format(new Date()));
		} else {
			map.put("date", date);
		}
		map.put("ids", ids.size() == 0 ? null : ids);
		List<PerformanceEntityU> list = psvr.getEntitieSearch(map);
		List<Integer> pids = new ArrayList<Integer>();
		for (PerformanceEntityU peu : list) {
			pids.add(peu.getId());
		}
		map.clear();
		map.put("pids", pids);
		map.put("status", status);
		psvr.updateEntitiesBy(map);
		LoginService loginsvr = this.springCtx.getBean(LoginService.class);
		zlId = loginsvr.getZLId(list.get(0).getCreater().getId());
		IMessageService msvr = this.springCtx.getBean(MessageService.class);
		MessageEntity me;

		if (status.equals("completed")) {
			me = new MessageEntity(0, zlId, "部门绩效被因强制比例不合格，被HR打回，请重新审核。", 0);
		} else {
			me = new MessageEntity(0, zlId, "部门绩效经HR审核通过。", 0);
		}
		msvr.add(me);
		json.put("status", "200");
		json.put("msg", "batch approval success");
		out.println(json);
		out.flush();
		out.close();
	}

	// 打回
	private void reject(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();

		json.put("status", "200");
		json.put("msg", "reject success");
		out.println(json);
		out.flush();
		out.close();
	}

	/**
	 * 绩效数据-获取所有的部门
	 * 
	 * @param req
	 * @param res
	 * @throws IOException
	 * @see [类、类#方法、类#成员]
	 */
	private void getAllBranch(HttpServletRequest req, HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		JSONObject json = new JSONObject();
		OrgService orgService = this.springCtx.getBean(OrgService.class);
		OrgEntity o = new OrgEntity();
		o.setParentOrgId(0);
		List<OrgEntity> list = orgService.getEntitiesBy(o);
		if (list != null && list.size() == 1) {
			o.setParentOrgId(list.get(0).getId());
			list = orgService.getEntitiesBy(o);
			json.put("status", "200");
			json.put("data", list);
		} else {
			json.put("status", "201");
		}
		out.println(json);
		out.flush();
		out.close();
	}
}
